package com.freedy.mall.order.listenner;

import com.freedy.common.to.mq.QuickOrderTo;
import com.freedy.mall.order.entity.OrderEntity;
import com.freedy.mall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Freedy
 * @date 2021/4/1 21:28
 */
@Slf4j
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSecKillListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(QuickOrderTo quickOrderTo, Message message, Channel channel) throws IOException {
        try {
            log.info("准备创建秒杀单的详细信息.....");
            orderService.createSecKillOrder(quickOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }
    }


}
